package com.bjgzy.rating.mvp.presenter;

import android.app.Application;

import com.bjgzy.rating.mvp.contract.SignUp3Contract;
import com.bjgzy.rating.mvp.model.entity.SignUp3Data;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.UploadData;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * @author xubo
 * @date 2018-8-26 17:08:16
 */
@ActivityScope
public class SignUp3Presenter extends BasePresenter<SignUp3Contract.Model, SignUp3Contract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SignUp3Presenter(SignUp3Contract.Model model, SignUp3Contract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void upload(SignUp3Data signUp3Data) {
        RxUtil.uploadImage(signUp3Data.getLocalMedia().getPath(),signUp3Data.getUrl(), ArmsUtils.obtainAppComponentFromContext(mRootView.getActivity()).repositoryManager())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
//                    mRootView.showUploadLoading(true);//显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
//                    mRootView.showUploadLoading(false);//隐藏下拉刷新的进度条
                })
                .subscribe(new ErrorHandleSubscriber<CommonData<UploadData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<UploadData> upload) {
                        if (upload.isResult()) {
                            signUp3Data.setUploadId(upload.getData().getId());
                        }

                    }
                });
    }

    public void saveFile(int researchId, int id, String jsonStr) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("worksId", id);
        map.put("json", jsonStr);
        mModel.saveFile(map)
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        if (commonData.isResult()) {
                            mRootView.update();
                        }else{
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }
}
